package com.shuma.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuma.common.vo.R;
import com.shuma.product.DTO.AllUserDTO;
import com.shuma.product.DTO.PerviewDTO;
import com.shuma.product.mapper.GoodsBaseInfoMapper;
import com.shuma.product.mapper.GoodsDetailInfoMapper;
import com.shuma.product.mapper.SalesOrExchangeMapper;
import com.shuma.product.mapper.UserMapper;
import com.shuma.product.pojo.GoodsBaseInfo;
import com.shuma.product.pojo.GoodsDetailInfo;
import com.shuma.product.pojo.SalesOrExchange;
import com.shuma.product.pojo.User;
import com.shuma.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yaoyuan
 * @date 2024年06月19日 下午12:04
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    GoodsDetailInfoMapper goodsDetailInfoMapper;

    @Autowired
    GoodsBaseInfoMapper goodsBaseInfoMapper;

    @Autowired
    SalesOrExchangeMapper salesOrExchangeMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public R preview(List<Integer> ids) {
        LambdaQueryWrapper<GoodsDetailInfo> detailInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<GoodsBaseInfo> baseInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SalesOrExchange> salesOrExchangeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 构造查询
        detailInfoLambdaQueryWrapper.in(GoodsDetailInfo::getGoodsDetailId, ids);
        baseInfoLambdaQueryWrapper.in(GoodsBaseInfo::getGoodsBaseId, ids);
        salesOrExchangeLambdaQueryWrapper.in(SalesOrExchange::getProductsId, ids);
        // 结果按id升序排序
        detailInfoLambdaQueryWrapper.orderByAsc(GoodsDetailInfo::getGoodsDetailId);
        baseInfoLambdaQueryWrapper.orderByAsc(GoodsBaseInfo::getGoodsBaseId);
        salesOrExchangeLambdaQueryWrapper.orderByAsc(SalesOrExchange::getProductsId);
        // 查询数据库记录
        List<GoodsDetailInfo> detailInfos = goodsDetailInfoMapper.selectList(detailInfoLambdaQueryWrapper);
        List<GoodsBaseInfo> baseInfos = goodsBaseInfoMapper.selectList(baseInfoLambdaQueryWrapper);
        List<SalesOrExchange> salesOrExchanges = salesOrExchangeMapper.selectList(salesOrExchangeLambdaQueryWrapper);
        //封装Json
        List<PerviewDTO> perviewDTOS = new ArrayList<>();
        for (int i = 0; i < detailInfos.size(); i++) {
            PerviewDTO perviewDTO = new PerviewDTO();
            perviewDTO.setGoodsId(detailInfos.get(i).getGoodsDetailId());
            perviewDTO.setGoodsAvatar(detailInfos.get(i).getGoodsAvatar());
            perviewDTO.setGoodCashPrice(detailInfos.get(i).getCashPrice());
            perviewDTO.setGoodIntegralPrice(detailInfos.get(i).getIntegralPrice());
            perviewDTO.setGoodsName(baseInfos.get(i).getGoodsName());
            perviewDTO.setGoodPriceType(detailInfos.get(i).getPriceType());
            perviewDTO.setTradeNum(salesOrExchanges.get(i).getSales());
            perviewDTOS.add(perviewDTO);
        }
        return R.ok(perviewDTOS);
    }

    @Override
    public R allUser() {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询可做代理人的用户(非管理员用户) 且 未被删除的用户 升序排序
        userLambdaQueryWrapper.eq(User::getRole,"user");
        userLambdaQueryWrapper.eq(User::getIsDelete,0);
        userLambdaQueryWrapper.orderByAsc(User::getUserId);
        List<User> users = userMapper.selectList(userLambdaQueryWrapper);
        //映射结果集
        List<AllUserDTO> allUserDTOS = new ArrayList<>();
        for (User user : users) {
            AllUserDTO allUserDTO = new AllUserDTO();
            allUserDTO.setUserId(user.getUserId());
            allUserDTO.setUserName(user.getUsername());
            allUserDTOS.add(allUserDTO);
        }
        return R.ok(allUserDTOS);
    }
}
